using UnityEngine;
using System.Collections;
using UnityEditor;

public class Player : MonoBehaviour {
	
	#region Variables
	
	#region Numbers
	
	private float timeJumping;
	private float jumpDislocation;
	
	

	#endregion
	
	#region Vectors
	//Used to import the wheels movement speed
	private Vector3 vectorPosition; //Note must use X for cabin but Z for imported Vector
	
	//Vector used to define how much the player will move from side to side will change once in the air
	public static Vector3 playerSideMove;
	
	private Vector3 previousPosition;
	#endregion
	
	
	#region Booleans
	//Used for collision dection
	private bool playerCollisionCabin;
	
	//Used to check the collision from the sides of the cabins
	private bool playerFalseCollision;
	
	private bool playerJump; 
	
	private bool superJumpKey1;
	private bool superJumpKey2;
	#endregion
	
	#region Testing variables
	
	#endregion
	
	#endregion
	
	// Use this for initialization
	void Start () {
		
		playerCollisionCabin	= false;
		playerFalseCollision	= false;
		playerSideMove.x		= 0.05f;  
		playerJump  			= false;
		timeJumping				= 0.01f;
		superJumpKey1 			= false;
		superJumpKey2			= false;
		vectorPosition = Vector3.zero;
	}
	
	// Update is called once per frame
	void Update () {
		
		
		
		superJumpKey1 			= false;
		superJumpKey2			= false;
		
		
		
		//Moves player along rotation of wheel
		if(playerCollisionCabin){
			
			playerSideMove.x=0.05f;  //Limits player movement from side to side while touching the top of the cabin
			vectorPosition.x =(  ( (Mathf.Deg2Rad * WheelRotation.Rotation.z)*12) + ( (10 / previousPosition.y)*1f /1750)  )*-1;
			
			
			//vectorPosition = transform.position;
			
			//vectorPosition = previousPosition;
			//vectorPosition.x += (cabin.Location.x- previousPosition.x);
			//transform.position = vectorPosition;
			
			//vectorPosition.x= (previousPosition.x-cabin.Location.x)*-1; //Must be reversed and scaled down for speed and direction
			transform.Translate(vectorPosition);
			
			
			//Code for jumping but only while it is touching the cabin
			if (  Input.GetKeyDown(KeyCode.J) ){
				superJumpKey1=true;
			}
			if (  Input.GetKeyDown(KeyCode.U) ){
				superJumpKey2=true;
			}
			if (  superJumpKey1 && superJumpKey2){
				jumpDislocation=( (50-transform.position.y) *0.01f);
				playerJump=true;
			}else if (Input.GetKeyDown(KeyCode.UpArrow))  {
				jumpDislocation= (50-transform.position.y)*0.005f;
				playerJump=true;
			}		
			
		}else if(!playerCollisionCabin){ //While Player is in the air alters players sideways movement speed to give more freedom
			
		playerSideMove.x=0.125f;
			
		}
		if(playerJump){
			if (timeJumping<3000f ){
				timeJumping+= timeJumping;
				transform.Translate(new Vector3(0,jumpDislocation,0));
				
			}else if (timeJumping>=3000f){
				timeJumping=0.2f;
				playerJump=false;
				
			
			}
		}
		
		#region Code for Dynamic Player movement
		
		if(!playerFalseCollision){   //Checks to see if player is touching sides if not it will let player move sideways
			
			if (Input.GetKey(KeyCode.LeftArrow)){
				
				transform.Translate(playerSideMove*-1);  //Has to be multiplied by a negative so that it will go left instead of right
				
			}else if (Input.GetKey(KeyCode.RightArrow)){
				
				transform.Translate(playerSideMove);
			}	
		}
			
		#endregion
		
		previousPosition = transform.position;
		
		
	}
	
	//Starts the player movement along the rotation
	void OnCollisionEnter(Collision collision){	
		
		
		
		//Makes the variable for cabin collision true to start the if for rotation along wheel
		if(collision.transform.tag=="cabin"){ playerCollisionCabin=true; }	
		
		//Makes the variable for collision on sides true so that it will skip the dynamic player movement
		if(collision.transform.tag=="falseCollision"){ playerFalseCollision=true;}
		
		
		
		
				
	}
	
	//Stops the player movement along rotation if the player jumps or falls from cabim
	void OnCollisionExit(Collision collision){
		
		//Makes the variable for cabin collision false to skip the if for rotation along wheel
		if (collision.transform.tag=="cabin"){
			playerCollisionCabin=false;
		}	
		
		//Makes the variable for collision on sides false so that it will start the dynamic player movement
		if(collision.transform.tag=="falseCollision"){
			playerFalseCollision=false;
		}
	}
}
